package example.demo.netty.channelHandler;

import example.demo.netty.JsonMessage;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.util.CharsetUtil;
import io.netty.util.ReferenceCountUtil;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@ChannelHandler.Sharable
public class ServerBusiChannelHandler extends SimpleChannelInboundHandler<JsonMessage> {
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        Channel channel = ctx.channel();
        System.out.println();
    }

//    @Override
//    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
//        if (msg instanceof JsonMessage) {
//            JsonMessage jsonMessage = (JsonMessage) msg;
//            jsonMessage.setType("server-response");
//            ctx.writeAndFlush(jsonMessage);
//            ReferenceCountUtil.release(msg);
//        } else {
//            ctx.fireChannelRead(msg);
//        }
//    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, JsonMessage msg) throws Exception {
        msg.setType("server-response");
        ctx.writeAndFlush(msg);
    }
}
